const app = getApp();
var api = require('../../api.js');

Page({
  data: {
    page:0
  },
  onLoad: function (options) {

    app.bind();
    this.setData({
      lesson_id: options.lessonId
    })
    console.log(this.data.lesson_id)
    this.getDetail();

  },
  onReady: function () {

  },
  onShow: function () {

  },
  onHide: function () {
    var url = app.getcurrentPageLessonId();
    wx.setStorageSync("from_url", url);
  },
  onUnload: function () {

  },
  onPullDownRefresh: function () {
    var page = this;

    page.getDetail();

    // 显示顶部刷新图标  
    wx.showNavigationBarLoading();

    // 隐藏导航栏加载框  
    wx.hideNavigationBarLoading();

    //停止下拉刷新
    wx.stopPullDownRefresh();
  },
  onReachBottom: function () {
    var that = this;
    // 显示加载图标  
    wx.showLoading({
      title: '玩命加载中',
    });

    if (that.data.page < that.data.count) {
      that.setData({
        page: that.data.page + 1
      });
    }

    that.getDetail();
    // 隐藏加载框  
    wx.hideLoading();
  },
  onShareAppMessage: function () {
    var url = app.getcurrentPageLessonId();
    var user_id = wx.getStorageSync('user_id');
    return {
      path: url + "&user_id=" + user_id,
      title: '得道1314'
    };  
  },

  //获取章节列表
  getDetail: function () {
    var that = this;
    var access_token = wx.getStorageSync('access_token');
    wx.request({
      url: api.index.lessonInfo,
      method: "get",
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        access_token: access_token,
        lesson_id: this.data.lesson_id,
        page: that.data.page
      },

      success: function (res) {
        console.log(res);
        if (res.data.code == 0) {
          that.setData({
            title: res.data.data.info.name,
            list: res.data.data.list,
            image: res.data.data.info.image,
            info: res.data.data.info,
            is_buy: res.data.data.info.is_buy,
            film_auth: res.data.data.film_auth,
            row_count: res.data.data.row_count
          })
          console.log(that.data.info)
          console.log(that.data.list)
          console.log(that.data.row_count);
          Math.ceil(that.data.row_count / 10);
          that.setData({
            count: Math.ceil(that.data.row_count / 10)
          })
          console.log(that.data.count)
          wx.setNavigationBarTitle({
            title: that.data.title
          })

         
        } else {
          wx.navigateTo({
            url: '/pages/grant/grant',
          })
        }

      }
    });
  },

  //跳转到详情
  toCourse: function (e) {
    console.log(e);
    var index = e.currentTarget.dataset.index;

    var proList = this.data.list;
    let str = JSON.stringify(proList[index]);
    console.log(str);
    var is_free = proList[index].is_free;
    console.log(is_free);

    if (this.data.film_auth == 1){
      wx.navigateTo({
        url: '/pages/moviesDetail/moviesDetail?str=' + str,
      })
    }else{
      wx.showModal({
        title: '提示',
        content: '您等级不够，请购买',
        cancelText: '暂不购买',
        confirmText: '立即购买',
        success: function (res) {
          if (res.confirm) {
            console.log('用户点击确定');
            wx.switchTab({
              url: '../../pages/user/upgrade/upgrade',
            })
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      });
    }
  },



  //课程介绍
  toArticle: function (e) {
    var proList = this.data.info;
    let str = JSON.stringify(proList);
    
    wx.navigateTo({
      url: '/pages/classIntroduce/classIntroduce?str=' +str
    })
  }


})